//
//  ViewController.m
//  17-homework-addressBook
//
//  Created by 王道林 on 16/8/8.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"
#import "ContectTableViewCell.h"
#import "Contecter.h"
#import "DetailViewController.h"
#import "AddViewController.h"
#import "SQLiteDataBaseManager.h"

#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource, AddViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *arrContect;
@property (nonatomic, strong) SQLiteDataBaseManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _manager = [SQLiteDataBaseManager shareManager];
    self.edgesForExtendedLayout = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    [self createSubView];
    
}

- (void)createData
{
    
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [arr firstObject];
    // 数据库SQLite3 文件类型,  .db
    NSString *dbPath = [document stringByAppendingPathComponent:@"Contact.db"];
    NSFileManager *manager = [NSFileManager defaultManager];
   BOOL is = [manager fileExistsAtPath:dbPath];
    if (is == 0) {
        
    _arrContect = [NSMutableArray array];
    NSArray *arrimage = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
 
    [_manager openSQlite3];
    [_manager crateTable];
    for (NSDictionary *dic in arr) {
        Contecter *contecter = [[Contecter alloc] init];
        [contecter setValuesForKeysWithDictionary:dic];
        contecter.picName = arrimage[arc4random() % 10];
        [_manager insertStu:contecter];
        
        [_arrContect addObject:contecter];
    }
    [_manager closeSQLite3];
    
    }
}

- (void)createSubView
{
    _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableview];
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [_tableview registerNib:[UINib nibWithNibName:@"ContectTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"wang"];
    _tableview.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    
    
    // 添加button
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStyleDone target:self action:@selector(addContect)];
    
    
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableview setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
   Contecter *contect = [_arrContect objectAtIndex:indexPath.row];
    [_manager openSQlite3];
    [_manager deleteStu:contect];
    [_manager closeSQLite3];
    [_arrContect removeObjectAtIndex:indexPath.row];
    
       [_tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}



- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrContect.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wang" forIndexPath:indexPath];
    Contecter *contecter = _arrContect[indexPath.row];
    
    cell.iamgeview.image = [UIImage imageNamed:contecter.picName];
    cell.nameLable.text = contecter.name;
    cell.phoneLable.text = contecter.phoneNumber;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Contecter *contect = _arrContect[indexPath.row];
    
    DetailViewController *VC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];
    VC.contect = contect;
    [self.navigationController pushViewController:VC animated:YES];
}

// 推出add界面

-(void)addContect
{
    AddViewController *AddVC = [[AddViewController alloc] initWithNibName:@"AddViewController" bundle:[NSBundle mainBundle]];
    AddVC.delegate = self;
    [self.navigationController pushViewController:AddVC animated:YES];
    
}


// 实现代理方法

- (void)passValue:(Contecter *)contect
{
    [_arrContect addObject:contect];
    [_manager openSQlite3];
    [_manager insertStu:contect];
    [_manager closeSQLite3];
       [_tableview reloadData];
}






- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 添加编辑
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    // 读取文件
    
    
    NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathDocument = [arrPath firstObject];
    NSLog(@"%@", pathDocument);
//    NSString *pathArr = [pathDocument stringByAppendingPathComponent:@"contect.plist"];
//    _arrContect = [NSKeyedUnarchiver unarchiveObjectWithFile:pathArr];

    [_manager openSQlite3];
    _arrContect = [_manager selectAll];
    [_manager closeSQLite3];
    NSLog(@"%@", _arrContect);
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
